package cyclequeue;
//牺牲一个空间实现队列
public class MyQueue2 {
    private int[] elem;
    private int front;
    private int rear;

    public MyQueue2() {
        this.elem = new int[10];
        front = rear = 0;
    }

    public boolean offer(int val) {
        if(isFull()) {
            return false;
        }
        elem[rear] = val;
        rear = (rear + 1) % elem.length;
        return true;
    }

    public boolean poll() {
        if(isEmpty()) {
            return false;
        }
        front = (front + 1) % elem.length;
        return true;
    }

    public int peekFront() {
        return elem[front];
    }

    public int peekRear() {
        return rear == 0 ? elem[elem.length-1] : elem[rear-1];
    }
    public boolean isEmpty() {
        return rear == front;
    }

    public boolean isFull() {
        return (rear + 1) % elem.length == front;
    }
}
